import db from "../models/index.js";
import {Op} from "sequelize";
let msg02 = db.msg_02;
let msg02Dao = {};
//获取某个sessionID的聊天记录
msg02Dao.getMessageListBySessionId = (_session_id, startTimestamp = 0) => {
  return msg02.findAndCountAll({
    where: {
      _session_id,
      _update_time:{[Op.gt]: startTimestamp},
    },
    raw: true,
  });
};
//添加某个sessionID的聊天记录
msg02Dao.addUserMsg = (_session_id, _sender, _timestamp, _message, _status, _update_time, t) => {
  if(!t) {
    return msg02.create({_session_id, _sender, _timestamp, _message, _status, _update_time});
  }
  return msg02.create({_session_id, _sender, _timestamp, _message, _status, _update_time}, {transaction:t});
};
//更新某个sessionID的聊天记录 比如撤回之类的
msg02Dao.updateUserMsg = (_session_id, _sender, _timestamp, _message, _status, _update_time, t) => {
  if(!t) {
    return msg02.update({_session_id, _sender, _timestamp, _message, _status, _update_time}, {where:{_session_id, _sender, _timestamp}});
  }
  return msg02.update({_session_id, _sender, _timestamp, _message, _status, _update_time}, {where:{_session_id, _sender, _timestamp}, transaction:t});
};
export default msg02Dao;
